﻿using MesGLApp.DAL.RoleDB;
using MesGLApp.DAL.RoleDTO;
using Smobiler.Core;
using Smobiler.Core.Controls;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MesGLApp.View
{
    partial class MainFrm : Smobiler.Core.Controls.MobileForm
    {
        #region "内部变量"

        #endregion

        public MainFrm() : base()
        {
            //This call is required by the SmobilerForm.
            InitializeComponent();
        }

        #region "控件事件"
        /// <summary>
        /// 界面读入时，根据权限创建菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MainFrm_Load(object sender, EventArgs e)
        {
            if (LoginFrm.MenuList != null && LoginFrm.MenuList.Count > 0)
            {
                this.InitParentToolBar(LoginFrm.MenuList);
                this.InitTabPage(LoginFrm.MenuList);
            }

            if (this.toolBar.Items.Count > 0)
            {
                toolBar.SelectedIndex = 0;
            }
        }
        #endregion

        #region "内部方法"
        /// <summary>
        /// 初始化工具菜单
        /// </summary>
        /// <param name="parentList"></param>
        private void InitParentToolBar(List<BaseMenuDTO> parentList)
        {
            //设定每个选项的图标
            Hashtable ht = this.InitItemImage();
            if (parentList != null && parentList.Count > 0)
            {
                //初始化工具栏
                foreach (BaseMenuDTO parent in parentList)
                {
                    ToolBarItem item = new ToolBarItem();
                    item.Tag = parent.id;
                    item.Name = parent.menu_code;
                    item.Text = parent.menu_name;
                    item.ImageType = ImageEx.ImageStyle.Image;
                    foreach (DictionaryEntry de in ht)
                    {
                        if (de.Key.ToString().Equals(item.Text))
                        {
                            List<string> tmpList = de.Value as List<string>;
                            item.IconID = tmpList[0];
                            item.SelectIconID = tmpList[1];
                        }
                    }
                    this.toolBar.Items.Add(item);
                }
            }
        }

        private void InitTabPage(List<BaseMenuDTO> parentList)
        {
            this.tabPageView1.Controls.Clear();
            if (parentList != null && parentList.Count > 0)
            {
                foreach (BaseMenuDTO parent in parentList)
                {
                    if (parent.class_name.Equals(""))
                    {
                        Smobiler.Core.Controls.Panel tmpPanel = new Smobiler.Core.Controls.Panel();
                        tmpPanel.Dock = System.Windows.Forms.DockStyle.Fill;
                        this.tabPageView1.Controls.Add(tmpPanel);
                    }
                    else
                    {
                        Type type = Type.GetType(parent.class_name);
                        if (type != null)
                        {
                            Smobiler.Core.Controls.MobileUserControl inst = null;
                            if (type.FullName.Equals("MesGLApp.View.MenuLayoutFrm"))
                            {
                                inst = Activator.CreateInstance(type, new Object[] { parent.menu_name }) as Smobiler.Core.Controls.MobileUserControl;
                            }
                            else
                            {
                                inst = Activator.CreateInstance(type) as Smobiler.Core.Controls.MobileUserControl;
                            }
                            this.tabPageView1.Controls.Add(inst);
                            inst.Dock = System.Windows.Forms.DockStyle.Fill;
                        }
                    }
                }
            }
        }

        private Hashtable InitItemImage()
        {
            Hashtable ht = new Hashtable();
            ht.Add("计划", new List<string>() { "plan_close", "plan_open" });
            ht.Add("制造", new List<string>() { "office_close", "office_open" });
            ht.Add("查询", new List<string>() { "report_close", "report_open" });
            ht.Add("我的", new List<string>() { "user_close", "user_open" });
            return ht;
        }
        #endregion

        

        private void toolBar_ToolbarItemClick(object sender, ToolbarClickEventArgs e)
        {
            if (toolBar.SelectedIndex != tabPageView1.PageIndex)
            {
                switch (e.Name)
                {
                    case "Menu_00025":
                        tabPageView1.PageIndex = 0;
                        break;
                    case "Menu_00026":
                        tabPageView1.PageIndex = 1;
                        break;
                    case "Menu_00027":
                        tabPageView1.PageIndex = 2;
                        break;
                    case "Menu_00028":
                        tabPageView1.PageIndex = 3;
                        break;
                }

            }
        }
    }
}